package com.hgyc.mom.eam.controller;

import java.io.UnsupportedEncodingException;
import java.util.*;

import com.hgyc.mom.common.context.FileServiceContext;
import com.hgyc.mom.common.util.http.HttpUtils;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.eam.entity.EamEquipment;
import com.hgyc.mom.eam.vo.EamEquipmentParam;
import com.hgyc.mom.eam.service.EamEquipmentService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 设备台账Controller
 *
 * @author fwj
 * @since 2025-08-10
 */
@Tag(name = "设备台账控制器", description = "设备台账控制器")
@RestController
@RequestMapping("/eam/equipment")
public class EamEquipmentController {
    @Autowired
    private EamEquipmentService eamEquipmentService;

    @Autowired
    private FileServiceContext fileServiceContext;

    @Operation(summary = "查询设备台账列表", description = "查询设备台账列表")
    @PostMapping("/pageList")
    public Result<IPage<EamEquipment>> pageList(@Parameter(description = "查询参数") @RequestBody EamEquipmentParam eamEquipmentParam) {
        Page<EamEquipment> page = new Page<>(eamEquipmentParam.getPage(), eamEquipmentParam.getPageSize());
        EamEquipment eamEquipment = new EamEquipment();
        BeanUtils.copyProperties(eamEquipmentParam, eamEquipment);
        QueryWrapper<EamEquipment> wrapper = QueryWrapperUtil.build(eamEquipment);
        IPage<EamEquipment> list = eamEquipmentService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取设备台账详细信息", description = "获取设备台账详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "设备台账ID") @PathVariable("id") Long id) {
        return Result.success(eamEquipmentService.getById(id));
    }
    @Operation(summary = "新增设备台账", description = "新增设备台账")
    @PostMapping
    public Result<?> save(@Parameter(description = "设备台账") @RequestBody EamEquipment eamEquipment) {
        eamEquipmentService.saveEquipment(eamEquipment);
        return Result.success();
    }

    @Operation(summary = "修改设备台账", description = "修改设备台账")
    @PutMapping
    public Result<?> update(@Parameter(description = "设备台账") @RequestBody EamEquipment eamEquipment) {
        eamEquipmentService.updateEquipment(eamEquipment);
        return Result.success();
    }

    @Operation(summary = "删除设备台账", description = "删除设备台账")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(eamEquipmentService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EamEquipment> util = new ExcelUtil<>(EamEquipment.class);
        util.importTemplateExcel(response, "设备台账");
    }

    @Operation(summary = "导出设备台账列表", description = "导出设备台账列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamEquipmentParam eamEquipmentParam) {
        EamEquipment eamEquipment = new EamEquipment();
        BeanUtils.copyProperties(eamEquipmentParam, eamEquipment);
        QueryWrapper<EamEquipment> wrapper = QueryWrapperUtil.build(eamEquipment);
        List<EamEquipment> list = eamEquipmentService.list(wrapper);
        ExcelUtil<EamEquipment> util = new ExcelUtil<>(EamEquipment.class);
        util.exportExcel(response, list, "设备台账数据");
    }

    @Operation(summary = "导入设备台账列表", description = "导入设备台账列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EamEquipment> util = new ExcelUtil<>(EamEquipment.class);
        List<EamEquipment> eamEquipmentList = util.importExcel(file.getInputStream());
        String message = eamEquipmentService.importData(eamEquipmentList, updateSupport);
        return Result.success(message);
    }
}